/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.att.config;

import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.ShellCommandUnresponsiveException;
import com.android.ddmlib.TimeoutException;
import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.FelEngineImpl;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lenovo
 */
public class CommandManager {
    private static FelEngine fel = new FelEngineImpl();
    private static OutputReceiver output = new OutputReceiver();
    
    public static List<String> touchCommand(
            String cmd, String x, String y, String xExpress, String yExpress) {
        List<String> list = new ArrayList<String>();
        int finalX = caculateExpress(xExpress.replaceFirst("x", x));
        int finalY = caculateExpress(yExpress.replaceFirst("y", y));
        cmd = cmd.replaceFirst("x", String.valueOf(finalX)).replaceFirst("y", String.valueOf(finalY));
        String[] array = splitCMD(cmd);
        list.addAll(Arrays.asList(array));
        return list;
    }
    
    public static List<String> dragCommand(
            String startCMD, String procedureCMD, String endCMD,
            String startX, String startY, 
            String endX, String endY, int steps, 
            String xExpress, String yExpress) {
        List<String> list = new ArrayList<String>();
        int finalStartX = caculateExpress(xExpress.replaceFirst("x", startX));
        int finalStartY = caculateExpress(yExpress.replaceFirst("y", startY));
        int finalEndX = caculateExpress(xExpress.replaceFirst("x", endX));
        int finalEndY = caculateExpress(yExpress.replaceFirst("y", endY));
        //start
        startCMD = startCMD.replaceFirst("startx", String.valueOf(finalStartX))
                .replaceFirst("starty", String.valueOf(finalStartY));
        String[] starts = splitCMD(startCMD);
        list.addAll(Arrays.asList(starts));
        //procedure
        int stepX = (finalEndX - finalStartX) / steps;
	int stepY = (finalEndY - finalStartY) / steps;
        for (int i = 0; i < steps; i++) {
            int _x = finalStartX + stepX * i;
            int _y = finalStartY + stepY * i;
            int procedureX = caculateExpress(xExpress.replaceFirst("x", String.valueOf(_x)));
            int procedureY = caculateExpress(yExpress.replaceFirst("y", String.valueOf(_y)));
            String _procedureCMD = procedureCMD.replaceFirst("x", String.valueOf(procedureX))
                    .replaceFirst("y", String.valueOf(procedureY));
            String[] procedures = splitCMD(_procedureCMD);
            list.addAll(Arrays.asList(procedures));
        }
        //end
        endCMD = endCMD.replaceFirst("endx", String.valueOf(finalEndX))
                .replaceFirst("endy", String.valueOf(finalEndY));
        String[] ends = splitCMD(endCMD);
        list.addAll(Arrays.asList(ends));
        return list;
    }
    
    public static List<String> LongpressCommand(
            String startCMD, String endCMD,
            String x, String y,
            String xExpress, String yExpress) {
        List<String> list = new ArrayList<String>();
        int finalX = caculateExpress(xExpress.replaceFirst("x", x));
        int finalY = caculateExpress(yExpress.replaceFirst("y", y));
        //start
        startCMD = startCMD.replaceFirst("startx", String.valueOf(finalX))
                .replaceFirst("starty", String.valueOf(finalY));
        String[] starts = splitCMD(startCMD);
        list.addAll(Arrays.asList(starts));
        //end
        endCMD = endCMD.replaceFirst("endx", String.valueOf(finalX))
                .replaceFirst("endy", String.valueOf(finalY));
        String[] ends = splitCMD(endCMD);
        list.addAll(Arrays.asList(ends));
        return list;
    }
    
    public static List<String> homeCommand(String cmd) {
        List<String> list = new ArrayList<String>();
        String[] array = splitCMD(cmd);
        list.addAll(Arrays.asList(array));
        return list;
    }
    
    public static List<String> menuCommand(String cmd) {
        List<String> list = new ArrayList<String>();
        String[] array = splitCMD(cmd);
        list.addAll(Arrays.asList(array));
        return list;
    }
    
    public static List<String> backCommand(String cmd) {
        List<String> list = new ArrayList<String>();
        String[] array = splitCMD(cmd);
        list.addAll(Arrays.asList(array));
        return list;
    }
    
    public static List<String> searchCommand(String cmd) {
        List<String> list = new ArrayList<String>();
        String[] array = splitCMD(cmd);
        list.addAll(Arrays.asList(array));
        return list;
    }
    
    public static void executeCommand(final List<String> cmds) {
        new Thread() {

            @Override
            public void run() {
//                long startTime = System.currentTimeMillis();
//                System.out.println(startTime);
//                try {
//                    Process pro = Runtime.getRuntime().exec("adb shell");
//                    OutputStream out = pro.getOutputStream();
//                    StringBuilder sb = new StringBuilder();
//                    for (String cmd : cmds) {
//                        sb.append(cmd);
//                        sb.append("\n");
//                    }
//                    byte[] b = sb.toString().getBytes();
//                    BufferedOutputStream bos = new BufferedOutputStream(out, b.length);
//                    bos.write(b);
//                    bos.flush();
//                } catch (IOException ex) {
//                    Logger.getLogger(CommandManager.class.getName()).log(Level.SEVERE, null, ex);
//                }
                
                
                IDevice device = AndroidProxy.getInstance().getDevice();
                for (String cmd : cmds) {
                    try {
                        device.executeShellCommand(cmd, output);
                    }  catch (IOException ex) {
                        Logger.getLogger(CommandManager.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (TimeoutException e) {
						e.printStackTrace();
					} catch (AdbCommandRejectedException e) {
						e.printStackTrace();
					} catch (ShellCommandUnresponsiveException e) {
						e.printStackTrace();
					}
                }
//                long endTime = System.currentTimeMillis();
//                System.out.println(endTime);
//                System.out.println(endTime - startTime);
            }
            
        }.start();
        
    }
    
    private static String[] splitCMD(String cmd) {
        return cmd.split("@\n");
    }
    
    private static String[] splitCMD1(String cmd) {
        return cmd.split("\n");
    }
    
    private static int caculateExpress(String express) {
//       return Integer.valueOf(fel.eval(express).toString());
       return Double.valueOf(fel.eval(express).toString()).intValue();
    }
}
